package com.match.competition_sub.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.competition_sub.entity.Course;
import com.match.competition_sub.excel.CourseData;
import com.match.competition_sub.service.CourseService;
import com.match.servicebase.exceptionhandler.AdiosException;


import java.util.List;

public class CourseExcelListener extends AnalysisEventListener<CourseData> {
    public CourseService courseService;
    public CourseExcelListener() {}
    public CourseExcelListener(CourseService courseService) {
         this.courseService = courseService;
        }

    @Override
    public void invoke(CourseData courseData, AnalysisContext analysisContext) {
        if (courseData == null) {
            throw new AdiosException(20001, "文件数据为空");
        }
        //判断添加的对象是否存在
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        courseQueryWrapper.eq("course_number", courseData.getCourseNumber());
        List<Course> list = courseService.list(courseQueryWrapper);
        if (!list.isEmpty()){
            //判断最终的返回结果
            Boolean b=finallyResult(list,courseData);
           if (b) {
                  return;
          } else {
              // 如果课程不为空直接导入数据
              Course course2=new Course();
              save(course2, courseData);
          }
        }else{
           Course course1=new Course();
           save(course1,courseData);
        }
    }

    //最终结果
    private Boolean finallyResult(List<Course> list, CourseData courseData) {
        Boolean b = null;
        for (Course course : list) {
             b=finallyResult(course,courseData);
             if(b) {
                 break;
             }
        }
        return b;
    }
    private Boolean finallyResult(Course course, CourseData courseData) {
        if(course.getCourseNumber().equals(courseData.getCourseNumber())
                && course.getCourseName().equals(courseData.getCourseName())
                && course.getCourseType().equals(courseData.getCourseType())
                && course.getCreditHour().equals(courseData.getCreditHour())
                && course.getSetTerm().equals(courseData.getSetTerm())
                && course.getGrade().equals(courseData.getGrade())
                && course.getMajorNumber().equals(courseData.getMajorNumber())
                && course.getMajorName().equals(courseData.getMajorName())
                && course.getDepartment().equals(courseData.getDepartment())){
                return true;
        }else {
                return false;
        }
    }


    private void save(Course course, CourseData courseData){
        //如果课程不为空直接导入数据
        course.setCourseNumber(courseData.getCourseNumber());
        course.setCourseName(courseData.getCourseName());
        course.setCourseType(courseData.getCourseType());
        course.setCreditHour(courseData.getCreditHour());
        course.setSetTerm(courseData.getSetTerm());
        course.setGrade(courseData.getGrade());
        course.setMajorNumber(courseData.getMajorNumber());
        course.setMajorName(courseData.getMajorName());
        course.setDepartment(courseData.getDepartment());
        courseService.save(course);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}


